package com.sa.oauth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sa.oauth.entity.SysPerson;
import com.sa.oauth.mapper.SysPersonMapper;
import com.sa.oauth.service.Oauth2UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class Oauth2UserServiceImpl implements Oauth2UserService {

    @Resource
    private SysPersonMapper sysPersonMapper;

    @Override
    public SysPerson getByAccount(String account) {
        if (StrUtil.isEmpty(account)) {
            return null;
        }
        return sysPersonMapper.selectByAccount(account);
    }

    @Override
    public SysPerson getById(long id) {
        SysPerson sysPerson = sysPersonMapper.selectById(id);
        return sysPerson;
    }

    @Override
    public SysPerson getByOpenid(String openid) {
        SysPerson sysPerson = sysPersonMapper.getByOpenid(openid);
        return sysPerson;
    }

    @Override
    public boolean verifyPassword(String account, String password) {
        SysPerson user = getByAccount(account);
        if (user == null) {
            return false;
        }

        // 判断账号状态是否正常
        if (user.getStatus() != null && user.getStatus() != 1) {
            log.warn("账号状态异常: {}", account);
            return false;
        }

        // 明文密码直接比较
        return user.getPassword().equals(password);

    }
}
